package cl.loso;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class MailServlet extends HttpServlet {
	private static final Logger logger = Logger.getLogger(MailServlet.class.getCanonicalName());
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String name = req.getParameter("nombre");
		String description = req.getParameter("mensaje");
		String email = req.getParameter("mail");
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);

		String msgBody = name + "\n" + description + "\n" + email;
		PrintWriter out = null;

		try {
			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress("marcelol@loso.cl", "Your admin"));
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
					"marcelol@loso.cl", "Marcelo"));
			msg.setSubject("Feedback");
			msg.setText(msgBody);
			Transport.send(msg);
			resp.setContentType("application/json");
			out = resp.getWriter();
			out.println("{");
	        out.println("\"error\": \"false\",");
	        out.println("\"msj\": \"Nos comunicaremos con usted\"");
	        out.println("}");			

		} catch (Exception e) {
			String error = e.getMessage();
		} finally {
			out.flush();
			out.close();
		}

	}
}
